<?php
require_once("../../../config.inc.php");
$isReloadable = true;
require_once("../../../app/_inc/include.php");
require_once("../_inc/procFunction.php");
require_once("./_inc/layoutFunction.php");

$errMsg = "";
if (sxREQ('reqtype')=='delete') {
	$errMsg .= layoutDel();
}
if (sxREQ('reqtype')=='delali') {
	$errMsg .= layoutAliasDel();
}

$sSQL = "SELECT * FROM layout_band WHERE layout='".sxREQ('layout')."' " .
		" ORDER BY band";
$query = $objSDAO->query($sSQL, "bands");

$sSQL = "SELECT * FROM layout_alias WHERE layout='".sxREQ('layout')."' " .
		" ORDER BY layout_alias";
$query = $objSDAO->query($sSQL, "aliases");

$sSQL = "SELECT l.* FROM layout l " .
			" WHERE layout='".sxREQ('layout')."' ";
$objSDAO->query($sSQL, "losel");
if (!$rowL = $objSDAO->fetch("losel")) {
	$errMsg .= _t("Layout '%s' missing",sxREQ('layout'))."<br>";
	$url = $_SESSION[USEROBJ]->getHistory(-1, true)."&msg=".htmlspecialchars($errMsg);
	serverRedir($url);
}

$aOpz = array("kMenu"=>'pro.lay', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
echo(bodyHTML($aOpz));
?>
<script type="text/javascript">
<!--
function confirmDelete() {
	if (confirm("Confirm ?")) {
		document.location.href = "layoutView.php?layout=<?=sxREQ('layout')?>&reqtype=delete&xsn=<?=XSN?>";
	}
}
function confDelAlias(alias) {
	if (confirm("Confirm delete alias '"+alias+"' ?")) {
		document.location.href = "layoutView.php?layout=<?=sxREQ('layout')?>&layout_alias="
			+alias+"&reqtype=delali&xsn=<?=XSN?>";
	}
}
//-->
</script>
<?php 

echo getSubHeader($rowL['module'],$rowL['sub_module']);

$aCmd = array();
$aCmd["modi"]=array("pag"=>"layoutMod.php?layout=$layout",
										"title"=>_t("change layout"),
										"text"=>_t("change"),
										"perm"=>"viewmod");
$aCmd["dele"]=array("pag"=>"javascript:confirmDelete();",
										"title"=>_t("delete layout"),
										"text"=>_t("delete"),
										"perm"=>"viewmod");
$aCmd["back"]=array();
$aCmd["lalf"]=array("pag"=>"layoutBandFieldList.php?layout=$layout",
										"title"=>_t("View all layout fields"),
										"text"=>_t("layout fields"),
										"perm"=>"viewmod",'align'=>'L');
$sHtmlCmd = commandsHTML($aCmd);	
echo $sHtmlCmd;	
echo("<table width=\"100%\" cellpadding=\"3\" cellspacing=\"2\" border='0'>\n");
echo("<tr>" .
			"<td class=\"titl01\" colspan='2'>Layout</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\" width=\"15%\">Name</td>" .
			"<td class=\"text01\" width=\"85%\">".$rowL['layout']."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Description</td>" .
			"<td class=\"text01\">".$rowL['description']."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Max colspan</td>" .
			"<td class=\"text01\">".$rowL['max_colspan']."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Display only</td>" .
			"<td class=\"text01\">".(($rowL['display_only']=='Y')?'Yes':'No')."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Flags</td>" .
			"<td class=\"text01\">".$rowL['flags']."</td>" .
		"</tr>\n");
require "../../_inc/aUpgradeStatus.php";
echo("<tr>" .
			"<td class=\"labl01\">Upgrade status</td>" .
			"<td class=\"text01\">".$rowL['upgrade_status']." - ".
			$aUpgradeStatus[$rowL['upgrade_status']]."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Note</td>" .
			"<td class=\"text01\"><pre class=\"text01\">".sxWW(sxHE($rowL['note']))."</pre></td>" .
		"</tr>\n");
echo("</table>\n");
//bands
if ($rowL['type'] == 'form') {
	echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
		$aCmd = array("pag"=>"layoutBandIns.php?layout=$layout",
												"title"=>_t("add new band"),
												"text"=>_t("new band"),
												"perm"=>"newmod");
		echo getTitleLink('Bands', $aCmd);
		echo("<tr>" .
			"<td class=\"tabh01\" width=\"1%\">&nbsp;</td>" .
			"<td class=\"tabh01\" width=\"1%\">Band</td>" .
			"<td class=\"tabh01\" width=\"40%\">Description</td>" .
			"<td class=\"tabh01\" width=\"18%\">&nbsp;</td>" .
			"<td class=\"tabh01\" width=\"40%\">Flags</td>" .
		"</tr>\n");	
	while($row = $objSDAO->fetch("bands")){
		echo("<tr>" .
			"<td class=\"text01\" valign='top'><a class=\"text01\" ".
				goToPag("layoutBandFieldList.php?layout=".$row["layout"]."&band=".$row["band"]."&xsn=".XSN).
				" title=\""._t("View fields")."\">campi</a></td>\n" .
			"<td class=\"text01\" valign='top'><a class=\"text01\" ".
				goToPag("layoutBandView.php?layout=".$row["layout"]."&band=".$row["band"]."&xsn=".XSN).
				" title=\""._t("View band")." '".sxHE($row["description"])."'\">".
				str_pad($row["band"],4,"0", STR_PAD_LEFT)."</a></td>\n" .
			"<td class=\"text01\" valign='top'>".sxHE($row["description"])."</td>" .
			"<td class=\"text01\" valign='top'>".(($row["display_only"]=='Y')?'display':'')."</td>" .
			"<td class=\"text01\" valign='top'>".sxHE($row["flags"])."</td>" .
		"</tr>\n");
	}
	echo("</table>\n");
}
echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
$aCmd = array("pag"=>"layoutAliasIns.php?layout=$layout",
										"title"=>_t("add new alias"),
										"text"=>_t("new alias"),
										"perm"=>"newmod");
echo getTitleLink('Aliases', $aCmd);
echo("<tr>" .
		"<td class=\"tabh01\" width=\"1%\">&nbsp;</td>" .
		"<td class=\"tabh01\" width=\"1%\">Alias</td>" .
		"<td class=\"tabh01\" width=\"98%\">Entity</td>" .
	"</tr>\n");	
while($row = $objSDAO->fetch("aliases")){
	echo("<tr>" .
			"<td class=\"text01\" valign='top'>");
	if ($row["layout_alias"]>'Q') {
		echo "&nbsp;";
	}else {
		echo("<a class=\"text01\" href=\"javascript:confDelAlias('".sxHE($row["layout_alias"])."');\">" .
				"<img src='".SERVER_URL_ROOT."/app/_img/b_drop.png' border='0'>".
				"</a>");
	}
	echo("</td>\n");
	echo("<td class=\"text01\" valign='top'>".$row["layout_alias"]."</td>");
	if (ereg("[WX]",$row["layout_alias"])) {
		echo("<td class=\"text01\" valign='top'>".
			"<a class='text01' ".goToPag("layoutSetVarList.php?set=$layout&alias=".$row["layout_alias"].
			"&xsn=".XSN).">Variables</a></td>");
	} else {
		echo("<td class=\"text01\" valign='top'>".$row["entity"]."</td>");
	}
	echo("</tr>\n");
}
echo("<tr><td class=\"text01\" colspan='3'>" .
		"<a class='text01' ".goToPag("layoutVarList.php?layout=$layout&layout_alias=W".
		"&xsn=".XSN).">W - simple memory variable</a>".
		"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;".
		"<a class='text01' ".goToPag("layoutVarList.php?layout=$layout&layout_alias=X".
		"&xsn=".XSN).">X - 1 dimension array memory variable</a>".
	"</td></tr>\n");

echo("</table>\n");
echo $sHtmlCmd;	
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function layoutDel(){
	global $objSDAO;
	$errMsg = "";
	$sSQL = "DELETE FROM layout_field " .
			"WHERE layout=".$_REQUEST["layout"]."";	
	$objSDAO->query($sSQL,"del");
	$sSQL = "DELETE FROM layout_band " .
			"WHERE layout=".$_REQUEST["layout"]."";	
	$objSDAO->query($sSQL,"del");
	$sSQL = "DELETE FROM layout " .
			"WHERE layout=".$_REQUEST["layout"]."";	
	$objSDAO->query($sSQL,"del");

	$sSQL = "DELETE FROM layout_alias " .
			"WHERE layout='$layout'";	
	$objSDAO->exec($sSQL);

	$sSQL = "DELETE FROM layout_variable " .
		"WHERE layout='$layout'";	
	$objSDAO->exec($sSQL);

	$sSQL = "DELETE FROM frame_alias_match " .
		"WHERE layout_set='$layout_set'";	
	$objSDAO->exec($sSQL);

	$sSQL = "DELETE FROM layout_field " .
		"WHERE layout='$layout' ";	
	$objSDAO->exec($sSQL);
	
	$url = $_SESSION[USEROBJ]->getHistory(-1, true);
	serverRedir($url);
	return 'abnormal end';
}

function layoutAliasDel(){
 	global $objSDAO;
	$errMsg = "";
	$layout = sxRAS("layout");
	$alias = sxRAS("layout_alias");
	$sSQL = "DELETE FROM layout_alias " .
			"WHERE layout='$layout' AND layout_alias='$alias'";	
	$objSDAO->exec($sSQL);

	$sSQL = "DELETE FROM layout_variable " .
		"WHERE layout='$layout' AND layout_alias='$alias'";	
	$objSDAO->exec($sSQL);

	$sSQL = "DELETE FROM frame_alias_match " .
		"WHERE layout_set='$layout_set' AND layout_alias='$alias'";	
	$objSDAO->exec($sSQL);

	$sSQL = "DELETE FROM layout_field " .
		"WHERE layout='$layout' AND layout_alias='$alias'";	
	$objSDAO->exec($sSQL);
	return '';
 } 
